package com.itheima.tanhua.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.tea.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.google.gson.JsonObject;
import com.itheima.commons.holder.TokenHolder;
import com.itheima.commons.utils.JwtUtils;
import com.itheima.tanhua.dubbo.api.UserService;
import com.itheima.tanhua.model.domain.User;
import com.itheima.tanhua.model.domain.UserInfo;
import com.itheima.tanhua.model.vo.BlackListVo;
import com.itheima.tanhua.model.vo.SettingsVo;
import io.jsonwebtoken.Claims;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UsersController {
    @DubboReference
    private UserService userService;

    /**
     * 查询用户
     * @param userId
     * @return
     */
    @GetMapping
    public UserInfo getUserInfo(@RequestParam(value = "userId",required = false)Long userId){
        if (StringUtils.isEmpty(userId)){
            long l = Integer.valueOf(TokenHolder.get()).longValue();
            userId=l;
        }

        return userService.getUserInfo(userId);
    }

    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){
        //获取id
        String id = TokenHolder.get();
        //设置id
        userInfo.setId(id);
        //更新操作
        boolean b = userService.updateUserInfo(userInfo);
        return b? ResponseEntity.ok("1") : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("修改失败");
    }

    /**
     * 用户通用设置 - 读取
     * @param authorization
     * @return
     */
    @GetMapping("/settings")
    public SettingsVo getsettings(@RequestHeader("Authorization")String authorization){
        Claims claims = JwtUtils.getClaims(authorization);
        Long id = Long.valueOf(claims.get("id").toString());
        String phone = claims.get("phone").toString();
        SettingsVo vo = userService.selectUserSettingsById(id);
        vo.setId(id);
        vo.setPhone(phone);
        return vo;
    }

    /**
     * 陌生人问题
     * @param authorization
     * @param content
     */
    @PostMapping("/questions")
    public void questions(@RequestHeader("Authorization")String authorization,@RequestBody String content){
        Claims claims = JwtUtils.getClaims(authorization);
        JSONObject jsonObject = JSONObject.parseObject(content);
        String txt = jsonObject.getString("content");
        Long id = Long.valueOf(claims.get("id").toString());
        userService.insertQuestion(txt,id);
    }
    /**
     * 通用设置-保存
     */
    @PostMapping("notifications/setting")
    public void saveSetting(@RequestHeader("Authorization")String authorization,@RequestBody Map map){
        //获取用户id
        Claims claims = JwtUtils.getClaims(authorization);
        Long id = Long.valueOf(claims.get("id").toString());
        String phone = claims.get("phone").toString();
        //获取设置的值
        boolean l = (boolean)map.get("likeNotification");
        boolean p = (boolean) map.get("pinglunNotification");
        boolean g = (boolean) map.get("gonggaoNotification");
        SettingsVo vo = new SettingsVo();
        vo.setId(id);
        vo.setPhone(phone);
        vo.setLikeNotification(l);
        vo.setPinglunNotification(p);
        vo.setGonggaoNotification(g);
        userService.saveSetting(vo);
    }
    @GetMapping("/blacklist")
    public List<BlackListVo> getBlacklist(@RequestHeader("Authorization")String authorization,@RequestParam(defaultValue = "0") Integer page,@RequestParam(defaultValue = "10")Integer pagesize){
        Claims claims = JwtUtils.getClaims(authorization);
        Integer id = (Integer) claims.get("id");
        long l = id.longValue();
        PageHelper.startPage(page,pagesize);
        return userService.getBlacklist(l);
    }
    @DeleteMapping("blacklist/{uid}")
    public ResponseEntity deleteBlackList(@RequestHeader("Authorization")String authorization,@PathVariable("uid") Long id){
        Claims claims = JwtUtils.getClaims(authorization);
        Integer uid = (Integer) claims.get("id");
        userService.deleteBlackList(uid,id);
        return ResponseEntity.ok(null);
    }

}
